﻿using DryIoc;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfForPrism模块.ViewModels
{
    internal class MainWindowViewModel : BindableBase
    {
        //区域管理服务
        private readonly IRegionManager _RegionManager;

        //对话框的管理服务
        private readonly IDialogService _DialogService;

        //记录访问顺序的导航栈
        private IRegionNavigationJournal _Journal;

        //事件聚合器
        private readonly IEventAggregator _EventAggregator;

        //实现切换的命令
        public DelegateCommand<string> ShowUCCmd { get; set; }

        //实现后退的命令
        public DelegateCommand BackCmd { get; set; }

        //实现打开对话框的命令
        public DelegateCommand<string> ShowDialogCmd { get; set; }

        //实现发布订阅取消订阅的命令
        public DelegateCommand SubCmd { get; set; }
        public DelegateCommand PubCmd { get; set; }
        public DelegateCommand UnSubCmd { get; set; }

        //属性
        private string backResult;

        public string BackResult
        {
            get { return backResult; }
            set
            {
                backResult = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowViewModel(IRegionManager RegionManager, IDialogService DialogService, IEventAggregator EventAggregator)
        {
            //依赖注入服务
            _RegionManager = RegionManager;
            _DialogService = DialogService;
            _EventAggregator = EventAggregator;

            //命令绑定具体方法
            ShowUCCmd = new DelegateCommand<string>(ShowUCFunc);
            BackCmd = new DelegateCommand(Back);
            ShowDialogCmd = new DelegateCommand<string>(ShowDialogFunc);
            PubCmd = new DelegateCommand(PubFunc);
            SubCmd = new DelegateCommand(SubFunc);
            UnSubCmd = new DelegateCommand(UnSubFunc);
        }

        //切换的具体方法
        private void ShowUCFunc(string viewName)
        {
            //接收参数
            var paras = new NavigationParameters();
            paras.Add("MsgA", "大家好！我是A");//键值对

            //导航并记录（通过重载中的回调函数）
            _RegionManager.RequestNavigate("Content", viewName,
                callback =>
            {
                //记录
                _Journal = callback.Context.NavigationService.Journal;
            },
                paras);
        }

        //后退的具体方法
        private void Back()
        {
            //判断是否能返回，如果刚打开或者之打开了一个页面就不能返回
            if (_Journal != null && _Journal.CanGoBack)
            {
                _Journal.GoBack();
            }
        }

        //显示对话框的具体方法
        private void ShowDialogFunc(string DialogName)
        {
            //进行对话框传参
            DialogParameters paras = new DialogParameters();
            paras.Add("Title", "传参的标题！");
            paras.Add("Para1", "P1");
            paras.Add("Para2", "P2");
            paras.Add("Para3", "P3");

            //打开对话框(没有两个参数的重载,必须要加一个回调函数)
            //这里回调函数的作用便是可以接受对话框传回来的数据
            _DialogService.ShowDialog(DialogName, paras, callback =>
            {
                if (callback.Result == ButtonResult.OK)
                {
                    BackResult = callback.Parameters.GetValue<string>("ConfirmPara");
                    return;
                }
                if (callback.Result == ButtonResult.No)
                {
                    BackResult = callback.Parameters.GetValue<string>("CancelPara");
                    return;
                }
                if (callback.Result == ButtonResult.None)
                {
                    BackResult = callback.Parameters.GetValue<string>("ClosePara");
                    return;
                }
            });
        }

        //发布订阅取消订阅的具体方法
        //订阅者需要先订阅某些消息，当发布者点击发布的时候，订阅者便收到了发布者传来的消息！
        private void SubFunc()
        {
            _EventAggregator.GetEvent<MsgEvent>().Subscribe(Sub);
        }

        private void Sub(string obj)
        {
            MessageBox.Show($"我收到的消息：{obj}");
        }

        private void PubFunc()
        {
            _EventAggregator.GetEvent<MsgEvent>().Publish("我想要去旅游！");
        }
        private void UnSubFunc()
        {
            _EventAggregator.GetEvent<MsgEvent>().Unsubscribe(Sub);
        }
    }
}
